package com.zhy.home.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zhy.home.feign.OrderFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class HomeController {
    @Resource
    private OrderFeignService orderFeignService;

    @Resource
    private RestTemplate restTemplate;

    private static final String GET_ORDER_INFO_URL = "http://localhost:3003/test";
    private static final String GET_ORDER_INFO_URL2 = "http://YUAN-CLOUD-ORDER/test";

    @GetMapping("/test")
    public String test() {
        return "Hello World";
    }

    @GetMapping("/getOrderInfo")
    public String getOrderInfo() {
        return restTemplate.getForObject(GET_ORDER_INFO_URL,String.class);
    }
    @GetMapping("/getOrderInfoE")
    public String getOrderInfoE() {
        return restTemplate.getForObject(GET_ORDER_INFO_URL2,String.class);
    }
    @GetMapping("/getOrderInfoV2")
    public String getOrderInfoV2() {
        return orderFeignService.test();
    }

    @GetMapping("/getOrderInfoTimeout")
    public String getOrderInfoTimeout() {
        return orderFeignService.testTimeout();
    }
    @GetMapping("/getHystrix")
    public String getHystrix() {
        return orderFeignService.testHystrix();
    }

    @GetMapping("/getHystrixTimeout")
    @HystrixCommand(fallbackMethod = "testHystrixTimeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "500")
    })
    public String getHystrixTimeout() {
        return orderFeignService.testHystrixTimeout();
    }

    public String testHystrixTimeoutHandler(){
        return "调用方进行主动降级！！！!";
    }
}
